//
//  WFPasswordModifyCell.m
//  WisdomFarm
//
//  Created by sky on 2017/12/21.
//  Copyright © 2017年 sky. All rights reserved.
//

#import "WFPasswordModifyCell.h"

@implementation WFPasswordModifyCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
    }
    return self;
}

- (void)setup {
    _titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_titleLabel];
    _passwordField = [[UITextField alloc] init];
    [self.contentView addSubview:_passwordField];
    _stateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_stateButton];
    [_stateButton addTarget:self action:@selector(onState) forControlEvents:UIControlEventTouchUpInside];
    [_stateButton setImage:[UIImage imageNamed:@"about_eye_closed"] forState:UIControlStateNormal];
    [_stateButton setImage:[UIImage imageNamed:@"about_eye_open"] forState:UIControlStateSelected];

    self.titleLabel.font = SCFont(14);
    self.titleLabel.textColor = [UIColor textTitleColor];
    
    self.passwordField.font = SCFont(14);
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.offset(0);
    }];
    
    [_passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(15);
        make.centerY.offset(0);
        make.right.lessThanOrEqualTo(self.stateButton.mas_left).offset(-15);
    }];
    
    [_stateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.centerY.offset(0);
        make.width.mas_equalTo(25);
    }];

    self.passwordVisible = NO;
}

- (void)setPasswordVisible:(BOOL)passwordVisible {
    _passwordVisible = passwordVisible;
    self.passwordField.secureTextEntry = !passwordVisible;
    self.stateButton.selected = passwordVisible;
}

- (void)onState {
    self.passwordVisible = !self.passwordVisible;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
